import {Component, OnInit} from '@angular/core';
import {Observable} from 'rxjs';
import {HttpClient, HttpHeaders} from '@angular/common/http';

@Component({
  selector: 'app-component',
  templateUrl: './component.component.html',
  styleUrls: ['./component.component.css']
})
export class ComponentComponent implements OnInit {

  /*  dataSource: Observable<any>;*/

  /*products: Array<any> = [];*/
  products: Observable<any>;

  constructor(private httpClient: HttpClient) {
    /*返回的是一个流信息*/
    /*this.dataSource = this.httpClient.get<any>('/api/products');*/
    /*前端异步请求 管道*/
    let myHeaders: HttpHeaders = new HttpHeaders();
    myHeaders.append('Authorization', 'Basic 123456');
    /*发送请求会多一个请求头*/
    this.products = this.httpClient.get<any>('/api/products', {headers: myHeaders});

  }

  ngOnInit() {
    /*请求是由get触发，而是有subscribe触发*/
    /* this.dataSource.subscribe(data => {
       this.products = data;
     });*/
  }

}
